package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿模块")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("添加地址")
    public Result add (@RequestBody AddressBook addressBook) {
        log.info("add addressBook: {}", addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }
    @GetMapping("list")
    @ApiOperation("得到用户所有地址")
    public Result list () {
        return Result.success(addressBookService.list());
    }

    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result defaultList () {
        AddressBook addressBook = addressBookService.find();
        return Result.success(addressBook);
    }

    @PutMapping
    @ApiOperation("地址更新")
    public Result update (@RequestBody AddressBook addressBook) {
        log.info("update addressBook: {}", addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result delete ( Long id) {
        log.info("delete addressBook: {}", id);
        addressBookService.delete(id);
        return Result.success();
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查找地址")
    public Result get (@PathVariable Long id) {
        log.info("get addressBook: {}", id);

        AddressBook addressBook=addressBookService.findById(id);
        return Result.success(addressBook);
    }

    @PutMapping("default")
    @ApiOperation("设置默认地址")
    public Result updateDefault (@RequestBody AddressBook addressBook) {
        log.info("update addressBook: {}", addressBook);
        addressBookService.updateDefault(addressBook.getId());
        return Result.success();
    }



}
